package com.zby.demo.controller.back;

import com.zby.demo.dao.NoticeDao;
import com.zby.demo.entity.Notice;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @Author ChrisPhantom
 * @CreateDate 2020/12/14 0:18
 */

@Controller
public class BackController {
    @Autowired
    NoticeDao noticeDao;


    /*
     * @Param: []
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/16 18:17
     * @Description: 请求转发到后台主页面
     */
    @RequestMapping({"/back", "/back/index.html"})
    public String toBack() {
        return "back/index";
    }

    /*
     * @Param: [model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/18 18:17
     * @Description: 请求转发到后台公告列表页面
     */
    @RequestMapping("/back/notice")
    public String toNotice(Model model) {
        List<Notice> notices = noticeDao.queryAllNotice();
        model.addAttribute("notices", notices);
        return "back/notice";
    }

    /*
     * @Param: [notice]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:20
     * @Description: 发布公告后重定向到公告列表
     */
    @PostMapping("/back/notice/add")
    public String addNotice(Notice notice) {
        noticeDao.addNotice(notice);
        return "redirect:/back/notice";
    }

    /*
     * @Param:
     * @Return:
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:23
     * @Description: 删除指定公告后重定向到公告列表
     */
    @GetMapping("/back/notice/delete/{noticeId}")
    public String deleteNotice(@PathVariable("noticeId") String noticeId) {
        noticeDao.deleteNotice(noticeId);
        return "redirect:/back/notice";
    }

    /*
     * @Param: [deleteall]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:26
     * @Description: 批量删除公告
     */
    @PostMapping("/back/notice/delete")
    public String deleteNotices(@Param("deleteall") String deleteall) {
        String[] deleteNotices = deleteall.split(",");
        for (String noticeId : deleteNotices) {
            noticeDao.deleteNotice(noticeId);
        }
        return "redirect:/back/notice";
    }

    /*
     * @Param: [notice]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:27
     * @Description: 修改公告内容
     */
    @PostMapping("/back/notice/info")
    public String updateNotice(Notice notice) {
        noticeDao.updateNotice(notice);
        return "redirect:/back/notice";
    }

    /*
     * @Param: []
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:27
     * @Description: 转发到帮助导入课程页面
     */
    @RequestMapping("/back/help/course")
    public String toHelpCourse() {
        return "back/help/course";
    }

    /*
     * @Param: []
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:28
     * @Description: 转发到帮助导入学生页面
     */
    @RequestMapping("/back/help/student")
    public String toHelpStudent() {
        return "back/help/student";
    }

    /*
     * @Param: []
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:28
     * @Description: 转发到帮助导入教师页面
     */
    @RequestMapping("/back/help/teacher")
    public String toHelpTeacher() {
        return "back/help/teacher";
    }
}
